package org.saxion.aktiehuis.view;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import org.saxion.aktiehuis.model.BesteldProduct;
import org.saxion.aktiehuis.model.Bestelling;
import org.saxion.aktiehuis.model.Product;
import org.saxion.aktiehuis.services.BestellingManagement;

import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class WinkelWagenView extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private BestellingManagement bestellingManagement;
	
    private JTable table;
    private JButton bevestigBtn;
    
    public WinkelWagenTabelModel model;
    private Bestelling bestelling;
    private int bestelId;
	private int klantId;
	
	/**
	 * Create the panel.
	 */
	public WinkelWagenView() {

		initialize();

	}
	

	
	private void initialize()
	{
		table = new JTable();
		bevestigBtn = new JButton("Bevestig bestelling");
		bevestigBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bevestigBestelling();
			}
		});
		
		add(new JScrollPane(table));
		add(bevestigBtn);
	}
	
	public void showBestelling()
	{
		
				bestelling = null;
		try {
			bestelling = bestellingManagement.getBestelling(bestelId);
		} catch (Exception e) {
			System.out.println("Error getting bestelling");
			e.printStackTrace();
		}

		if(bestelling != null){
			model = new WinkelWagenTabelModel(new ArrayList(bestelling.getProducten()));
			table.setModel(model);
		}
	}
	
	public void bevestigBestelling()
	{
		try {
			if(!bestelling.getBevestigd())
				bestellingManagement.bevestigBestelling(klantId, bestelId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Bestelling bevestig");
		
	}
	
	public void setBestellingManagement(BestellingManagement bestellingManagement) throws Exception
	{
		this.bestellingManagement = bestellingManagement;
	}

	public void setKlantId(int klantId) {
		this.klantId = klantId;
		
	}
	
	public void setBestelId(int id)
	{
		bestelId = id;
	}

}
